import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import Atuoplay from '@/views/Atuoplay.vue'
import { isLogin } from '@/http/api'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'atuo',
    alias: '/m',
    component: Atuoplay,
    redirect: '/m',
    children: [
      {
        path: '/m',
        name: 'home',
        component: () => import(/* webpackChunkName: "about" */ '../views/tabbar/HomeView.vue'),
      },
      {
        path: '/about',
        name: 'about',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '@/views/tabbar/AboutView.vue')
      },
      {
        path: '/my',
        name: 'my',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '@/views/tabbar/MyView.vue'),
        async beforeEnter(to, from, next) {
          let token = localStorage.getItem('token')
          if (token) {
            isLogin('/logged', { token })
              .then(
                data => {
                  // console.log(data);
                  if (data.code == 200) {
                    next();
                  } else {
                    next('/login')
                  }
                }
              )
          } else {
            next('/login')
          }
        }
      },

    ]
  },
  {
    path: '/shop',
    name: 'shop',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/tabbar/ShopView.vue'),
    async beforeEnter(to, from, next) {
      let token = localStorage.getItem('token')
      if (token) {
        isLogin('/logged', { token })
          .then(
            data => {
              // console.log(data);
              if (data.code == 200) {
                next();
              } else {
                next('/login')
              }
            }
          )
      } else {
        next('/login')
      }
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/My/login.vue')
  },
  {
    path: '/enroll',
    name: 'enroll',
    component: () => import('@/views/My/enroll.vue')
  },
  {
    path: '/modify',
    name: 'modify',
    component: () => import('@/views/My/modify.vue')
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import('@/views/goods/GoodsView.vue')
  },
  {
    path: '/address',
    name: 'address',
    component: () => import('@/views/address/AddressView.vue')
  },
  {
    path: '/payment',
    name: 'payment',
    component: () => import('@/views/payment/PaymentView.vue')
  },
  {
    path: '/allorder',
    name: 'allorder',
    component: () => import('@/views/order/AllorderView.vue')
  },
  
]

const router = new VueRouter({
  routes
})

export default router
